package com.stu.serive.impl;

import com.stu.Dto.ScholarshipStuInfortmationDto;
import com.stu.constant.ScholarshipInformationConstant;
import com.stu.constant.StuScoreAssessConstant;
import com.stu.context.BaseContext;
import com.stu.mapper.ScholarshipInformationMapper;
import com.stu.mapper.ScholarshipMapper;
import com.stu.mapper.StuScoreMapper;
import com.stu.pojo.Scholarship;
import com.stu.pojo.ScholarshipStuInformation;
import com.stu.pojo.StuScore;
import com.stu.serive.ScholarshipService;
import com.stu.vo.ScholarshipStuInformationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ScholarshipServiceImpl implements ScholarshipService {

    @Autowired
    private ScholarshipMapper scholarshipMapper;
    @Autowired
    private StuScoreMapper stuScoreMapper;
    @Autowired
    private ScholarshipInformationMapper scholarshipInformationMapper;

    /**
     * 查询奖学金基础信息
     * @return
     */
    public List<Scholarship> selectScholarship() {
        List<Scholarship> scholarships = scholarshipMapper.selectScholarship();
        //根据学生的成绩对开放状态进行修改（设计成绩表，成绩表包含所需数据）
        //获取当前学生的id
        Long currentId = BaseContext.getCurrentId();

        //根据学生表id查询成绩表获取数据
        StuScore stuScore = stuScoreMapper.selectByBasicId(currentId);
        log.info("stuScore_date:{}",stuScore);

        //获取班级学生数量以便判断班级排名
        Integer rank = stuScoreMapper.selectRankByBasicId(currentId);
        log.info("rank:{}",rank);

        //获取该学生班级的人数
        Integer class_number = stuScoreMapper.selectClassNumberByStuClass(stuScore.getStuClass());
        log.info("class_number:{}",class_number);

        //查询该学生是否在违纪名单中
        Integer violation = stuScoreMapper.selectViolationByBasicID(currentId);

        //对奖学金限制进行判断
        //1、特等奖
        if(
            //专业文化素质评分大于等于90
            stuScore.getProfessionalScore() >= StuScoreAssessConstant.specialScholarship.professional_score
            //身体心理素质大于等于80
                    && stuScore.getPhysicalPsychological() >= StuScoreAssessConstant.specialScholarship.physicalPsychological
            //思想道德素质大于等于90
                    && stuScore.getIdeologyMorality() >= StuScoreAssessConstant.specialScholarship.ideologyMorality
            //最低成绩大于等于60
                    && stuScore.getMinScore() >= StuScoreAssessConstant.specialScholarship.minScore
            //无违规行为
                    && violation == null
        ){
            scholarships.get(0).setSwitchStatus(1);

            //根据申请情况，对申请状态进行修改 -已申请或未申请（设计一个申请表，若学生在表内，则表明学生已经申请了该项奖学金）
            String scholarshipType = StuScoreAssessConstant.specialScholarship.scholarshipType;
            Integer application_status = scholarshipInformationMapper.selectByBasicId(currentId, scholarshipType);
            log.info("application_status:{}",application_status);
            if(application_status != null){
                scholarships.get(0).setApplicationStatus(1);
            }
        }
        //得到1、2、3等奖所需的最低排名
        double firstRank=1;
        double secondRank=1;
        double thirdRank=1;
        if(class_number * StuScoreAssessConstant.firstScholarship.rankRatio >=1){
            firstRank = class_number * StuScoreAssessConstant.firstScholarship.rankRatio;
        }
        log.info("一等获奖排名低于或等于:{}",firstRank);

        if(class_number * StuScoreAssessConstant.firstScholarship.rankRatio >=1){
            secondRank = class_number * StuScoreAssessConstant.secondScholarship.rankRatio;
        }
        log.info("二等获奖排名低于或等于:{}",secondRank);

        if(class_number * StuScoreAssessConstant.firstScholarship.rankRatio >=1){
            thirdRank = class_number * StuScoreAssessConstant.thirdScholarship.rankRatio;
        }
        log.info("三等获奖排名低于或等于:{}",thirdRank);

        //2、一等奖
        if(
            //最低成绩大于等于60
            stuScore.getMinScore() >= StuScoreAssessConstant.firstScholarship.minScore
                //班级排名前3%
                && rank <= firstRank && violation == null){
            //修改申请状态为可申请
            scholarships.get(1).setSwitchStatus(1);
            //根据申请情况，对申请状态进行修改 -已申请或未申请（设计一个申请表，若学生在表内，则表明学生已经申请了该项奖学金）
            String scholarshipType = StuScoreAssessConstant.firstScholarship.scholarshipType;
            Integer application_status = scholarshipInformationMapper.selectByBasicId(currentId, scholarshipType);
            if(application_status != null){
                scholarships.get(1).setApplicationStatus(1);
            }
        }
        //3、二等奖
        if(  //最低成绩大于等于60
                stuScore.getMinScore() >= StuScoreAssessConstant.secondScholarship.minScore
                        //班级排名前3%
                        && rank <= secondRank && violation == null){
            //修改申请状态为可申请
            scholarships.get(2).setSwitchStatus(1);
            //根据申请情况，对申请状态进行修改 -已申请或未申请（设计一个申请表，若学生在表内，则表明学生已经申请了该项奖学金）
            String scholarshipType = StuScoreAssessConstant.secondScholarship.scholarshipType;
            Integer application_status = scholarshipInformationMapper.selectByBasicId(currentId, scholarshipType);
            if(application_status != null){
                scholarships.get(2).setApplicationStatus(1);
            }
        }
        //4、三等奖
        if(  //最低成绩大于等于60
                stuScore.getMinScore() >= StuScoreAssessConstant.thirdScholarship.minScore
                        //班级排名前3%
                        && rank <= thirdRank && violation == null){
            //修改申请状态为可申请
            scholarships.get(3).setSwitchStatus(1);
            //根据申请情况，对申请状态进行修改 -已申请或未申请（设计一个申请表，若学生在表内，则表明学生已经申请了该项奖学金）
            String scholarshipType = StuScoreAssessConstant.thirdScholarship.scholarshipType;
            Integer application_status = scholarshipInformationMapper.selectByBasicId(currentId, scholarshipType);
            if(application_status != null){
                scholarships.get(3).setApplicationStatus(1);
            }
        }
        return scholarships;
    }

    /**
     * 查询已经申请的奖学金项目的情况
     * @return
     */
    public List<ScholarshipStuInformationVo> selectScholarshipStuInformation() {

        Long currentId = BaseContext.getCurrentId();

        List<ScholarshipStuInformation> scholarshipStuIfoList = scholarshipInformationMapper.selectAllByBasicId(currentId);
        //log.info("scholarshipStuIfoVo:{}",scholarshipStuIfoVo);

        //创建对应Vo类List
        List<ScholarshipStuInformationVo> scholarshipStuInformationVoList = new ArrayList<>();

        //对scholarshipStuIfoList进行遍历，将数据赋值给Vo
        for (ScholarshipStuInformation stuInfo : scholarshipStuIfoList) {
            // 创建新的Vo对象
            ScholarshipStuInformationVo vo = new ScholarshipStuInformationVo();
            //开始赋值
            BeanUtils.copyProperties(stuInfo, vo);
            //判断该项目是否审核通过
            if ( stuInfo.getAdminExamine() == 1){
                vo.setExamineStatus("审核通过");
            }else{
                vo.setExamineStatus("审核未通过");
            }
            //将最后的数据添加进scholarshipStuInformationVoList中
            scholarshipStuInformationVoList.add(vo);
        }
        return scholarshipStuInformationVoList;
    }

    /**
     * 插入申请的奖学金项目
     * @param scholarshipStuInfortmationDto
     */
    public void insertScholarship(ScholarshipStuInfortmationDto scholarshipStuInfortmationDto) {
        Long currentId = BaseContext.getCurrentId();
        //获取申请时间
        LocalDateTime time = LocalDateTime.now();
        ScholarshipStuInformation sI = new ScholarshipStuInformation();
        BeanUtils.copyProperties(scholarshipStuInfortmationDto, sI);
        //设置stu_id
        sI.setStuId(Math.toIntExact(currentId));
        //设置申请时间
        sI.setApplicationTime(time);
        //设置奖项周期
        sI.setAwardCycle("2023-2024");
        //设置奖金
        if (Objects.equals(sI.getScholarshipType(), "特等奖学金")){
            sI.setMoney("3000");
        } else if (Objects.equals(sI.getScholarshipType(), "三等奖学金")) {
            sI.setMoney("1500");
        } else if (Objects.equals(sI.getScholarshipType(), "二等奖学金")) {
            sI.setMoney("700");
        } else {
            sI.setMoney("300");
        }

        scholarshipMapper.insertScholarship(sI);
    }

    /**
     * 查询所有奖学金信息
     * @return
     */
    public List<ScholarshipStuInformationVo> selectScholarshipStuInformationAll() {
        return scholarshipMapper.selectScholarshipAll();
    }
}
